#include "ECS/System/PMaterialSystem.h"
#include "ECS/Component/PLookAtCameraComponent.h"

#include "PiccoloApplication.h"

#include "Render/PiccoloRenderContext.h"
#include "Render/PiccoloRenderTarget.h"

namespace Piccolo
{
    PiccoloApplication* PMaterialSystem::GetApplication() const
    {
        PiccoloAppContext* appContext = PiccoloApplication::GetAppContext();
        if (appContext)
        {
            return appContext->app;
        }
        return nullptr;
    }

    PiccoloVKDevice* PMaterialSystem::GetDevice() const
    {
        PiccoloAppContext* appContext = PiccoloApplication::GetAppContext();
        if (appContext && appContext->renderContext)
        {
            return appContext->renderContext->GetDevice();
        }
        return nullptr;
    }

    PScene * PMaterialSystem::GetScene() const
    {
        PiccoloAppContext* appContext = PiccoloApplication::GetAppContext();
        if (appContext)
        {
            return appContext->scene;
        }
        return nullptr;
    }

    const glm::mat4 PMaterialSystem::GetProjMatrix(PiccoloRenderTarget *renderTarget) const
    {
        glm::mat4 projMatrix{ 1.f };
        PEntity* camera = renderTarget->GetCamera();
        if (PEntity::HasComponent<PLookAtCameraComponent>(camera))
        {
            auto& cameraComponent = camera->GetComponent<PLookAtCameraComponent>();
            projMatrix = cameraComponent.GetProjMatrix();
        }
        return projMatrix;
    }

    const glm::mat4 PMaterialSystem::GetViewMatrix(PiccoloRenderTarget *renderTarget) const
    {
        glm::mat4 viewMatrix{ 1.f };
        PEntity* camera = renderTarget->GetCamera();
        if (PEntity::HasComponent<PLookAtCameraComponent>(camera))
        {
            auto& cameraComponent = camera->GetComponent<PLookAtCameraComponent>();
            viewMatrix = cameraComponent.GetViewMatrix();
        }
        return viewMatrix;
    }
}
